package com.example.easyutils.openprize;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.easyutils.BaseActivity;
import com.example.easyutils.R;
import com.example.easyutils.httputil.HttpGetRequest;
import com.example.easyutils.joke.JokeActivity;
import com.example.easyutils.joke.JokeRecycleView;
import com.example.easyutils.util.MyRecycleViewAdapter;
import com.example.easyutils.util.MyTypeAdapter;
import com.example.easyutils.watermark.ClearWatermark;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.w3c.dom.Text;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class OpenCode extends BaseActivity {
    private RecyclerView showRecycleView;
    private GridView openGridView;
    private LinearLayoutManager mLinearLayoutManager;
    private static final int ERROR_CODE = 1,SUCCESS_CODE = 2,WARN_CODE = 3;
    private List openCodeList = null;

    Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case ERROR_CODE:
                    break;
                case SUCCESS_CODE:
                    Log.d("listNum",openCodeList+"");

                    if(openCodeList!=null){
                        Log.d("showRecycleView",showRecycleView+"");
                        mLinearLayoutManager = new LinearLayoutManager(OpenCode.this,RecyclerView.VERTICAL,false);
                        OpenRecycleView openRecycleView = new OpenRecycleView(openCodeList,OpenCode.this,"showCode");
                        showRecycleView.setLayoutManager(mLinearLayoutManager);
                        showRecycleView.setAdapter(openRecycleView);
                    }


                    break;
                case WARN_CODE:

                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_code);
        showRecycleView = (RecyclerView)findViewById(R.id.showRecycleView);

        Log.d("showRecycleView",showRecycleView+"");

        final List mList = new ArrayList();
        initData(mList);

//        getOpenCode("dlt");
        openGridView = (GridView) findViewById(R.id.openCodeGridView);
        SimpleAdapter adapter = new SimpleAdapter(OpenCode.this,mList,
                R.layout.open_code_item,new String[]{"image","text"},new int[]{R.id.imageView,R.id.textView});
        openGridView.setAdapter(adapter);
        openGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                getOpenCode(((Map)mList.get(position)).get("code").toString());
            }
        });


//        openRecycleView.setLayoutManager(new GridLayoutManager(this,3));
//        openRecycleView.setAdapter(new OpenRecycleView(mList, OpenCode.this,"showTitle"));


    }


    public void initData(List list){
        String textList[] = new String[]{"七星彩","排列五","排列三","双色球","七乐彩","福彩3d","大乐透",
                "六场半全场","四场进球彩","十四场胜负彩(任9)"};
        String codeList[] = new String[]{"qxc","pl5","pl3","ssq","qlc","fc3d","dlt","zcbqc","zcjqc","zcsfc"};
        int imageList[] = new int[]{R.drawable.jisuqixingcai,R.drawable.pailiewu,R.drawable.pailiesan,
                R.drawable.shuangseqiu,R.drawable.qlc,R.drawable.fucaid,R.drawable.daletou,
                R.drawable.liu,R.drawable.si,R.drawable.shisi};


        for(int i = 0; i < imageList.length; i ++){
            Map map = new HashMap();
            map.put("image",imageList[i]);
            map.put("text",textList[i]);
            map.put("code",codeList[i]);
            list.add(map);
        }
    }
//dlt  福彩3d fc3d  排列3 pl3  pl5  七乐彩qlx  qxc
// 双色球 ssq  六场半全场zcbqc  四场进球彩 zcjqc
    //十四场胜负彩(任9) zcsfc


    private class getOpenCode extends Thread{
        public void run(){

        }
    }

    public void getOpenCode(final String type){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpGetRequest.sendOkHttpGetRequst("http://f.apiplus.net/newly.do?format=json&code="+type,
                        new okhttp3.Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                Log.d("ERROR",e+"");
                                handler.sendEmptyMessage(ERROR_CODE);
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                String result = response.body().string();
//                                Log.d("SUCCESS",result);
                                Gson gson = new GsonBuilder()
                                        .registerTypeAdapter(new TypeToken<Map<String, List>>(){

                                        }.getType(),new MyTypeAdapter()).create();
                                Map<String,List> dataDetail = gson.fromJson(result,new TypeToken<Map<String,List>>(){

                                }.getType());
                                openCodeList = new ArrayList();
                                openCodeList = dataDetail.get("data");
                                handler.sendEmptyMessage(SUCCESS_CODE);
                            }
                        });

            }
        }).start();
//        http://f.apiplus.net/newly.do?code=plw&format=json

    }
}
